package csc.fresher.finalgroupfour.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import csc.fresher.finalgroupfour.domain.Account;
import csc.fresher.finalgroupfour.domain.Customer;

@Repository("accountDAO")
public class AccountDAO {
	@PersistenceContext
	private EntityManager em; // Entity Manager is acquired by container.
	
	/**
	 * Add a new account.
	 * @param account
	 */
	@Transactional
	public void addAccount(Account account) {
		em.persist(account);
	}
	
	/**
	 * List all accounts in DB.
	 * @return
	 */
	@Transactional
	public List<Account> listAccounts() {
		List<Account> accounts = new ArrayList<Account>();
		TypedQuery<Account> query = em.createQuery("SELECT a FROM Account a", Account.class);
		
		accounts = query.getResultList();
		if (accounts.size() > 0) {
			return accounts;
		}
		return null;
	}
	
	/**
	 * Update an account's information
	 * @param account
	 */
	@Transactional
	public void updateAccount(Account account) {
		em.merge(account);
	}
	
	/**
	 * Remove an account from database.
	 * @param account
	 */
	@Transactional
	public void deleteAccount(Account account) {
		em.remove(account);
	}
	
	/**
	 * Find weather an account existed or not.
	 * @param account
	 * @return
	 */
	@Transactional
	public List<Account> findAccounts(Customer customer) {
		//TypedQuery<Account> query = em.createQuery("", Account.class);
		return null;
	}
}
